package com.halflife.framework.generator.template.mapper;

/**
 * Created by zhaoyf on 2018/6/18.
 */
public class InsertSelectiveAnalysis extends BaseAnalysis {
    public InsertSelectiveAnalysis(Class clazz) {
        super(clazz);
        this.isTwoAnalysis =true;
    }

    @Override
    protected void setPrefix() {
        this.xmlStr.append("<insert id=\"insertSelective\" keyProperty=\"id\" useGeneratedKeys=\"true\" parameterType=\""+this.clazz.getName()+"\">\n");
        this.xmlStr.append("\t insert into `"+this.tableName+"` \n");
        this.xmlStr.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n");

    }
    protected void comma() {

    }

    @Override
    protected String getValue1(CustomField customField) {
        if (!customField.isPersist()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("<if test=\""+customField.getName()+" != null\">\n");
        sb.append("\t\t").append(customField.getRealName()).append(",\n");
        sb.append("\t</if>\n");
        return sb.toString();
    }

    protected void middleAnaysis(){
        xmlStr.append("</trim>\n");
        xmlStr.append("<trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\">\n");
    }
    protected String getValue2(CustomField customField) {
        if (!customField.isPersist()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("<if test=\""+customField.getName()+" != null\">\n");
        sb.append("\t\t#{").append(customField.getName()).append(",jdbcType=").append(customField.getTypeName()).append("},\n");
        sb.append("\t</if>\n");
        return sb.toString();
    }

    ;

    protected void setSuffix() {

        this.xmlStr.append( "</trim> \n </insert>");
    }

    ;
}
